package com.shengdou.as.slopsapptest.utils;

/**
 * Created by AS on 2016/11/19.
 * 存储数据信息
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;


import com.shengdou.as.slopsapptest.receiver.BuildConfigs;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class FileUtils {
    private static final String TAG = "FileUtils";
    /**
     * 存储头像路径
     */
    public static final String ROOT = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();// 主文件夹
    public static final String XUEZI_PATH = ROOT + "/shengdoudianzi/";
    public static final String IMAGES_PATH = XUEZI_PATH + "images/";


    /**
     * 根据时间创建文件名㿍
     *
     * @return “img_xxxxxxx.jpg‿
     */
    public static String createImageFileName() {
        String name = "img_" + System.currentTimeMillis() + ".jpg";
        return name;
    }


    public static String createImageFileName2() {
        String name = "img_headlogo" + ".jpg";
        return name;
    }

    /* 判断SD卡是否存在 返回true表示存在 */
    public  static boolean avaiableMedia() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }
    // 获取文件的保存路径
    public static File getDownAppFile(Context context) throws Exception {
        File path = getAlbumStorageDir(context, "com.shengdou.application");
        File file = new File(path.getAbsolutePath() + "/slopsapp.apk");
        if (BuildConfigs.DEBUG){
            Log.e(TAG, file.getPath().toString());
        }
        if (!path.exists()) {
            path.mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }
    public static File getAlbumStorageDir(Context context, String albumName) {
        File file = new File(context.getExternalFilesDir(
                Environment.DIRECTORY_DOWNLOADS), albumName);
        file.mkdirs();
        return file;
    }
    /**
     * 保存用户头像
     */
    public static boolean savehead(Bitmap bm) {// id为用户唯一标示
        if (bm == null) {
            return false;
        }
        try {
            // 判断路径是否存在，不存在则新建路径
            // 创建一个位于SD卡上的文件

            File file = new File(IMAGES_PATH);
            if (!file.exists()) {
                file.mkdirs();
            }
            if (file.exists()) {
                file.createNewFile();
            }
            File file1 = new File(file, createImageFileName2());
            /*FileOutputStream outStream = null;
            // 打开指定文件对应的输出流
            outStream = new FileOutputStream(file1);
            // 把位图输出到指定文件中
            bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.close();*/
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
            bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            bos.close();
            System.out.println("图片保存成功");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.toString() + "保存错误");
            return false;
        }
    }


    public static boolean saveObject(Context context, String name, Object sod) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = context.openFileOutput(name, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(sod);
        } catch (Exception e) {
            e.printStackTrace();
            //这里是保存文件产生异常
            return false;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    //fos流关闭异常
                    e.printStackTrace();
                }
            }
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    //oos流关闭异常
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    public static Object getObject(Context context, String name) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = context.openFileInput(name);
            ois = new ObjectInputStream(fis);
            return ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            //这里是读取文件产生异常
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    //fis流关闭异常
                    e.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    //ois流关闭异常
                    e.printStackTrace();
                }
            }
        }
        //读取产生异常，返回null
        return null;
    }

}
